/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConsultaPlanoContas.java
 *
 * Created on 23/09/2009, 12:29:02
 */
package planocontas.telas;

import contaspagar.telas.FrmCadastraPagar;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import planocontas.bd.PlanoContasDAO;
import planocontas.classes.ConsultaPlanoContasTableModel;
import planocontas.classes.PlanoContas;
import utilitarios.ItelaConsulta;

/**
 *
 * @author Wolverine
 */
public class FrmConsultaPlanoContas extends javax.swing.JFrame{
    FrmCadastraPagar cadastraPagar;
    public FrmConsultaPlanoContas(ItelaConsulta telaConsulta) {
        try {
            planoContasDAO = new PlanoContasDAO();
            List<PlanoContas> listaPlanoContas = planoContasDAO.getPlanoContas();
            model = new ConsultaPlanoContasTableModel(listaPlanoContas);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());

            model = new ConsultaPlanoContasTableModel(new ArrayList<PlanoContas>());
        }
        this.cadastraPagar = (FrmCadastraPagar) telaConsulta;
        initComponents();
        this.setLocationRelativeTo(null);
        setVisible(true);
        consultaPlanoContasjTable.setModel(model);
        consultaPlanoContasjTable.getColumnModel().getColumn(0).setMaxWidth(60);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PlanoContasjPanel = new javax.swing.JPanel();
        descricaoPlanoContasjTextField = new javax.swing.JTextField();
        descricaojLabel = new javax.swing.JLabel();
        BarradeTarefasjToolBar = new javax.swing.JToolBar();
        adicionarPlanoContasjButton = new javax.swing.JButton();
        editarPlanoContasjButton = new javax.swing.JButton();
        excluirPlanoContasjButton = new javax.swing.JButton();
        SeparadordeTarefasjSeparator = new javax.swing.JToolBar.Separator();
        imprimirjButton = new javax.swing.JButton();
        VoltarjButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        consultaPlanoContasjTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta Plano de Contas");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        PlanoContasjPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PlanoContasjPanel.setPreferredSize(new java.awt.Dimension(800, 600));
        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        PlanoContasjPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        descricaoPlanoContasjTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descricaoPlanoContasjTextFieldKeyPressed(evt);
            }
        });
        PlanoContasjPanel.add(descricaoPlanoContasjTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 410, -1));

        descricaojLabel.setText("Descrição");
        PlanoContasjPanel.add(descricaojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        BarradeTarefasjToolBar.setFloatable(false);
        BarradeTarefasjToolBar.setRollover(true);

        adicionarPlanoContasjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/contamais.png"))); // NOI18N
        adicionarPlanoContasjButton.setText("Adicionar");
        adicionarPlanoContasjButton.setToolTipText("Adicionar Plano de Contas");
        adicionarPlanoContasjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        adicionarPlanoContasjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        adicionarPlanoContasjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        adicionarPlanoContasjButton.setName(""); // NOI18N
        adicionarPlanoContasjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        adicionarPlanoContasjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        adicionarPlanoContasjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarPlanoContasjButtonActionPerformed(evt);
            }
        });
        BarradeTarefasjToolBar.add(adicionarPlanoContasjButton);

        editarPlanoContasjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/contaedit.png"))); // NOI18N
        editarPlanoContasjButton.setText("Editar");
        editarPlanoContasjButton.setToolTipText("Editar Plano de Contas");
        editarPlanoContasjButton.setEnabled(false);
        editarPlanoContasjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editarPlanoContasjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        editarPlanoContasjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        editarPlanoContasjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        editarPlanoContasjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editarPlanoContasjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarPlanoContasjButtonActionPerformed(evt);
            }
        });
        BarradeTarefasjToolBar.add(editarPlanoContasjButton);

        excluirPlanoContasjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/contamenos.png"))); // NOI18N
        excluirPlanoContasjButton.setText("Excluir");
        excluirPlanoContasjButton.setToolTipText("Excluir Plano de Contas");
        excluirPlanoContasjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        excluirPlanoContasjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        excluirPlanoContasjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        excluirPlanoContasjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        excluirPlanoContasjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        excluirPlanoContasjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirPlanoContasjButtonActionPerformed(evt);
            }
        });
        BarradeTarefasjToolBar.add(excluirPlanoContasjButton);
        BarradeTarefasjToolBar.add(SeparadordeTarefasjSeparator);

        imprimirjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/impressao.png"))); // NOI18N
        imprimirjButton.setText("Imprimir");
        imprimirjButton.setToolTipText("Imprimir");
        imprimirjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        imprimirjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        imprimirjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        imprimirjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        imprimirjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        BarradeTarefasjToolBar.add(imprimirjButton);

        VoltarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        VoltarjButton.setText("Voltar");
        VoltarjButton.setToolTipText("Voltar");
        VoltarjButton.setFocusable(false);
        VoltarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        VoltarjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        VoltarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarTelajButtonActionPerformed(evt);
            }
        });
        BarradeTarefasjToolBar.add(VoltarjButton);

        PlanoContasjPanel.add(BarradeTarefasjToolBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 430, -1));

        consultaPlanoContasjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Título 1", "Título 2"
            }
        ));
        consultaPlanoContasjTable.getTableHeader().setReorderingAllowed(false);
        consultaPlanoContasjTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                consultaPlanoContasjTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(consultaPlanoContasjTable);

        PlanoContasjPanel.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 410, 250));

        getContentPane().add(PlanoContasjPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 460, 430));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void descricaoPlanoContasjTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descricaoPlanoContasjTextFieldKeyPressed
        String planoContas = descricaoPlanoContasjTextField.getText();
        List listaPlanoContas = planoContasDAO.pesquisaPlanoContas(planoContas);
        model.listaPlanoContas = listaPlanoContas;
        model.fireTableDataChanged();
    }//GEN-LAST:event_descricaoPlanoContasjTextFieldKeyPressed
    private void adicionarPlanoContasjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarPlanoContasjButtonActionPerformed
        new FrmCadastraPlanoContas(new PlanoContas());
}//GEN-LAST:event_adicionarPlanoContasjButtonActionPerformed
    private void editarPlanoContasjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarPlanoContasjButtonActionPerformed
        int linha = consultaPlanoContasjTable.getSelectedRow();
        if (linha >= 0) {
            PlanoContas planoContas = model.listaPlanoContas.get(linha);
            new FrmCadastraPlanoContas(planoContas);
        }
}//GEN-LAST:event_editarPlanoContasjButtonActionPerformed
    private void excluirPlanoContasjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirPlanoContasjButtonActionPerformed
        int linha = consultaPlanoContasjTable.getSelectedRow();
        if (linha >= 0) {
            PlanoContas planoContas = model.listaPlanoContas.get(linha);
            try {
                planoContasDAO.excluir(planoContas);
                JOptionPane.showMessageDialog(this, "Dados excluidos com sucesso");
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Dados não foram excluidos");
            }
        }
}//GEN-LAST:event_excluirPlanoContasjButtonActionPerformed
    private void voltarTelajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voltarTelajButtonActionPerformed
        this.dispose();
}//GEN-LAST:event_voltarTelajButtonActionPerformed
    private void consultaPlanoContasjTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_consultaPlanoContasjTableMouseClicked
        if (evt.getClickCount() == 1) {
            editarPlanoContasjButton.setEnabled(true);
        }else if (evt.getClickCount() == 2 && cadastraPagar != null) {
            int linha = consultaPlanoContasjTable.getSelectedRow();
            if (linha >= 0) {
                PlanoContas planoContas = model.listaPlanoContas.get(linha);
                cadastraPagar.setData(planoContas);
                this.dispose();
            }
        }
}//GEN-LAST:event_consultaPlanoContasjTableMouseClicked
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar BarradeTarefasjToolBar;
    private javax.swing.JPanel PlanoContasjPanel;
    private javax.swing.JToolBar.Separator SeparadordeTarefasjSeparator;
    private javax.swing.JButton VoltarjButton;
    private javax.swing.JButton adicionarPlanoContasjButton;
    private javax.swing.JTable consultaPlanoContasjTable;
    private javax.swing.JTextField descricaoPlanoContasjTextField;
    private javax.swing.JLabel descricaojLabel;
    private javax.swing.JButton editarPlanoContasjButton;
    private javax.swing.JButton excluirPlanoContasjButton;
    private javax.swing.JButton imprimirjButton;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    private PlanoContasDAO planoContasDAO;
    private ConsultaPlanoContasTableModel model;
}
